package com.qiantu.phone.ui.fragment;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.db.LLRoomDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.RoomBean;
import com.qiantu.phone.R;
import com.qiantu.phone.action.StatusAction;
import com.qiantu.phone.app.TitleBarFragment;
import com.qiantu.phone.bean.RoomEnvironmentalTypeBean;
import com.qiantu.phone.ui.activity.HomeActivity;
import com.qiantu.phone.ui.adapter.RoomEnvironmentalTypeAdapter;
import com.qiantu.phone.widget.StatusLayout;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * time   : 2021/12/22
 * desc   : 我的 Fragment
 */
public final class IndoorFragment extends TitleBarFragment<HomeActivity> implements StatusAction {

    private StatusLayout mStatusLayout;
    private RecyclerView recyclerView;
    private RoomEnvironmentalTypeAdapter mRoomEnvironmentalTypeAdapter;
    private RoomEnvironmentalTypeBean.EnvironmentalType temperatureType;
    private RoomEnvironmentalTypeBean.EnvironmentalType humidityType;
    private RoomEnvironmentalTypeBean.EnvironmentalType quailityType;
    private RoomEnvironmentalTypeBean.EnvironmentalType humanBodyInductionType;
    private RoomEnvironmentalTypeBean.EnvironmentalType humanPresenceInductionType;
    private RoomEnvironmentalTypeBean.EnvironmentalType noiseType;
    private RoomEnvironmentalTypeBean.EnvironmentalType illuminanceType;
    private RoomEnvironmentalTypeBean.EnvironmentalType pm25Type;
    private RoomEnvironmentalTypeBean.EnvironmentalType doorContactType;
    private RoomEnvironmentalTypeBean.EnvironmentalType toxicGasType;
    private RoomEnvironmentalTypeBean.EnvironmentalType smokeAlarmType;
    private RoomEnvironmentalTypeBean.EnvironmentalType triggerSwitchType;
    private RoomEnvironmentalTypeBean.EnvironmentalType alarmSwitchType;
    private RoomEnvironmentalTypeBean.EnvironmentalType humanbodySenoroType;
    private RoomEnvironmentalTypeBean.EnvironmentalType infraredContactType;
    private RoomEnvironmentalTypeBean.EnvironmentalType vibrationSensingType;
    private RoomEnvironmentalTypeBean.EnvironmentalType infraredCurtainType;

    public static IndoorFragment newInstance() {
        return new IndoorFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.indoor_fragment;
    }

    @Override
    protected void initView() {
        mStatusLayout = findViewById(R.id.status_layout);
        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRoomEnvironmentalTypeAdapter = new RoomEnvironmentalTypeAdapter(getContext());
        recyclerView.setAdapter(mRoomEnvironmentalTypeAdapter);
    }

    @Override
    protected void initData() {
        showLoading();
        Observable.create(new ObservableOnSubscribe<List<RoomEnvironmentalTypeBean>>() {
            @Override
            public void subscribe(ObservableEmitter<List<RoomEnvironmentalTypeBean>> emitter) throws Exception {
                List<RoomBean> rooms = LLRoomDBManager.getInstance(getContext()).getAll();
                List<RoomEnvironmentalTypeBean> roomEnvironmentalTypes = new ArrayList<>();
                List<RoomEnvironmentalTypeBean.EnvironmentalType> commonEnvironmentalTypes = getEnvironmentalTypes(LLDeviceDBManager.getInstance(getContext()).getAvailableCommonDevices());
                if(commonEnvironmentalTypes != null && commonEnvironmentalTypes.size() > 0){
                    //通用房间
                    RoomEnvironmentalTypeBean roomEnvironmentalType = new RoomEnvironmentalTypeBean();
                    roomEnvironmentalType.setRoomSerialNo(RoomBean.ROOM_COMMON_SERIALNO);
                    roomEnvironmentalType.setFloorRoomName(getString(R.string.common));
                    roomEnvironmentalType.setEnvironmentalTypes(commonEnvironmentalTypes);
                    roomEnvironmentalTypes.add(roomEnvironmentalType);
                }
                for(RoomBean room : rooms){
                    List<DeviceBean> devices = room.getDevices();
                    //每个房间先重置下环境类型
                    temperatureType = null;
                    humidityType = null;
                    quailityType = null;
                    humanBodyInductionType = null;
                    humanPresenceInductionType = null;
                    noiseType = null;
                    illuminanceType = null;
                    pm25Type = null;
                    doorContactType = null;
                    toxicGasType = null;
                    smokeAlarmType = null;
                    triggerSwitchType = null;
                    alarmSwitchType = null;
                    humanbodySenoroType = null;
                    infraredContactType = null;
                    vibrationSensingType = null;
                    infraredCurtainType = null;
                    List<RoomEnvironmentalTypeBean.EnvironmentalType> environmentalTypes = getEnvironmentalTypes(devices);
                    if(environmentalTypes != null && environmentalTypes.size() > 0){
                        RoomEnvironmentalTypeBean roomEnvironmentalType = new RoomEnvironmentalTypeBean();
                        roomEnvironmentalType.setRoomSerialNo(room.getRoomSerialNo());
                        roomEnvironmentalType.setFloorRoomName(LLRoomDBManager.getInstance(getContext()).getFloorRoomName(room.getRoomSerialNo()));
                        roomEnvironmentalType.setEnvironmentalTypes(environmentalTypes);
                        roomEnvironmentalTypes.add(roomEnvironmentalType);
                    }
                }
                //每个房间先重置下环境类型
                temperatureType = null;
                humidityType = null;
                quailityType = null;
                humanBodyInductionType = null;
                humanPresenceInductionType = null;
                noiseType = null;
                illuminanceType = null;
                pm25Type = null;
                doorContactType = null;
                toxicGasType = null;
                smokeAlarmType = null;
                triggerSwitchType = null;
                alarmSwitchType = null;
                humanbodySenoroType = null;
                infraredContactType = null;
                vibrationSensingType = null;
                infraredCurtainType = null;
                emitter.onNext(roomEnvironmentalTypes);
            }
        }).subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<RoomEnvironmentalTypeBean>>() {
                    @Override
                    public void accept(List<RoomEnvironmentalTypeBean> roomEnvironmentalTypes) throws Exception {
                        mRoomEnvironmentalTypeAdapter.setData(roomEnvironmentalTypes);
                        showComplete();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                    }
                });
        

    }

    private List<RoomEnvironmentalTypeBean.EnvironmentalType> getEnvironmentalTypes(List<DeviceBean> devices){
        if(devices != null && devices.size() > 0){
            List<RoomEnvironmentalTypeBean.EnvironmentalType> environmentalTypes = new ArrayList<>();
            for(DeviceBean device : devices){
                if(device.getEnvironmentType() == 0){
                    continue;
                }
                switch (device.getEnvironmentType()){
                    case 2:
                        if(temperatureType == null){
                            temperatureType = new RoomEnvironmentalTypeBean.EnvironmentalType();
                            temperatureType.setDeviceName(device.getName());
                            temperatureType.setDeviceSerialNo(device.getDeviceSerialNo());
                            temperatureType.setType(device.getEnvironmentType());
                            temperatureType.setTypeName(getString(R.string.temperature));
                            temperatureType.setTypeIcon(R.attr.icon_intelligent_environment_temperature);
                            environmentalTypes.add(temperatureType);
                        }
                        break;
                    case 3:
                        if(humidityType == null){
                            humidityType = new RoomEnvironmentalTypeBean.EnvironmentalType();
                            humidityType.setDeviceName(device.getName());
                            humidityType.setDeviceSerialNo(device.getDeviceSerialNo());
                            humidityType.setType(device.getEnvironmentType());
                            humidityType.setTypeName(getString(R.string.humidity));
                            humidityType.setTypeIcon(R.attr.icon_intelligent_environment_humidity);
                            environmentalTypes.add(humidityType);
                        }
                        break;
                    case 5:
                        if(quailityType == null){
                            quailityType = new RoomEnvironmentalTypeBean.EnvironmentalType();
                            quailityType.setDeviceName(device.getName());
                            quailityType.setDeviceSerialNo(device.getDeviceSerialNo());
                            quailityType.setType(device.getEnvironmentType());
                            quailityType.setTypeName(getString(R.string.air_quaility));
                            quailityType.setTypeIcon(R.attr.icon_intelligent_environment_pm25);
                            environmentalTypes.add(pm25Type);
                        }
                        break;
                    case 9:
                        if(humanBodyInductionType == null){
                            humanBodyInductionType = new RoomEnvironmentalTypeBean.EnvironmentalType();
                            humanBodyInductionType.setDeviceName(device.getName());
                            humanBodyInductionType.setDeviceSerialNo(device.getDeviceSerialNo());
                            humanBodyInductionType.setType(device.getEnvironmentType());
                            humanBodyInductionType.setTypeName(getString(R.string.human_body_induction));
                            humanBodyInductionType.setTypeIcon(R.attr.icon_intelligent_environment_pir);
                            environmentalTypes.add(humanBodyInductionType);
                        }
                        break;
                    case 10:
                        if(humanPresenceInductionType == null){
                            humanPresenceInductionType = new RoomEnvironmentalTypeBean.EnvironmentalType();
                            humanPresenceInductionType.setDeviceName(device.getName());
                            humanPresenceInductionType.setDeviceSerialNo(device.getDeviceSerialNo());
                            humanPresenceInductionType.setType(device.getEnvironmentType());
                            humanPresenceInductionType.setTypeName(getString(R.string.human_presence_induction));
                            humanPresenceInductionType.setTypeIcon(R.attr.icon_intelligent_environment_pir2);
                            environmentalTypes.add(humanPresenceInductionType);
                        }
                        break;
                    case 12:
                        if(noiseType == null){
                            noiseType = new RoomEnvironmentalTypeBean.EnvironmentalType();
                            noiseType.setDeviceName(device.getName());
                            noiseType.setDeviceSerialNo(device.getDeviceSerialNo());
                            noiseType.setType(device.getEnvironmentType());
                            noiseType.setTypeName(getString(R.string.noise));
                            noiseType.setTypeIcon(R.attr.icon_intelligent_environment_noise);
                            environmentalTypes.add(noiseType);
                        }
                        break;
                    case 13:
                        if(illuminanceType == null){
                            illuminanceType = new RoomEnvironmentalTypeBean.EnvironmentalType();
                            illuminanceType.setDeviceName(device.getName());
                            illuminanceType.setDeviceSerialNo(device.getDeviceSerialNo());
                            illuminanceType.setType(device.getEnvironmentType());
                            illuminanceType.setTypeName(getString(R.string.illuminance));
                            illuminanceType.setTypeIcon(R.attr.icon_intelligent_environment_illumination);
                            environmentalTypes.add(illuminanceType);
                        }
                        break;
                    case 14:
                        if(pm25Type == null){
                            pm25Type = new RoomEnvironmentalTypeBean.EnvironmentalType();
                            pm25Type.setDeviceName(device.getName());
                            pm25Type.setDeviceSerialNo(device.getDeviceSerialNo());
                            pm25Type.setType(device.getEnvironmentType());
                            pm25Type.setTypeName(getString(R.string.pm25));
                            pm25Type.setTypeIcon(R.attr.icon_intelligent_environment_pm2_5);
                            environmentalTypes.add(pm25Type);
                        }
                        break;
                    case 16:
                        if(doorContactType == null){
                            doorContactType = new RoomEnvironmentalTypeBean.EnvironmentalType();
                            doorContactType.setDeviceName(device.getName());
                            doorContactType.setDeviceSerialNo(device.getDeviceSerialNo());
                            doorContactType.setType(device.getEnvironmentType());
                            doorContactType.setTypeName(getString(R.string.door_contact));
                            doorContactType.setTypeIcon(R.attr.icon_intelligent_environment_gatemagnetism_linkage);
                            environmentalTypes.add(doorContactType);
                        }
                        break;
                    case 17:
                        if(toxicGasType == null){
                            toxicGasType = new RoomEnvironmentalTypeBean.EnvironmentalType();
                            toxicGasType.setDeviceName(device.getName());
                            toxicGasType.setDeviceSerialNo(device.getDeviceSerialNo());
                            toxicGasType.setType(device.getEnvironmentType());
                            toxicGasType.setTypeName(getString(R.string.toxic_gas));
                            toxicGasType.setTypeIcon(R.attr.icon_intelligent_environment_gas_linkage);
                            environmentalTypes.add(toxicGasType);
                        }
                        break;
                    case 18:
                        if(smokeAlarmType == null){
                            smokeAlarmType = new RoomEnvironmentalTypeBean.EnvironmentalType();
                            smokeAlarmType.setDeviceName(device.getName());
                            smokeAlarmType.setDeviceSerialNo(device.getDeviceSerialNo());
                            smokeAlarmType.setType(device.getEnvironmentType());
                            smokeAlarmType.setTypeName(getString(R.string.smoke_alarm));
                            smokeAlarmType.setTypeIcon(R.attr.icon_intelligent_environment_smog_linkage);
                            environmentalTypes.add(smokeAlarmType);
                        }
                        break;
                    case 19:
                        if(triggerSwitchType == null){
                            triggerSwitchType = new RoomEnvironmentalTypeBean.EnvironmentalType();
                            triggerSwitchType.setDeviceName(device.getName());
                            triggerSwitchType.setDeviceSerialNo(device.getDeviceSerialNo());
                            triggerSwitchType.setType(device.getEnvironmentType());
                            triggerSwitchType.setTypeName(getString(R.string.trigger_switch));
                            triggerSwitchType.setTypeIcon(R.attr.icon_intelligent_environment_trigger_linkage);
                            environmentalTypes.add(triggerSwitchType);
                        }
                        break;
                    case 20:
                        if(alarmSwitchType == null){
                            alarmSwitchType = new RoomEnvironmentalTypeBean.EnvironmentalType();
                            alarmSwitchType.setDeviceName(device.getName());
                            alarmSwitchType.setDeviceSerialNo(device.getDeviceSerialNo());
                            alarmSwitchType.setType(device.getEnvironmentType());
                            alarmSwitchType.setTypeName(getString(R.string.alarm_switch));
                            alarmSwitchType.setTypeIcon(R.attr.icon_intelligent_environment_alarm_linkage);
                            environmentalTypes.add(alarmSwitchType);
                        }
                        break;
                    case 21:
                        if(humanbodySenoroType == null){
                            humanbodySenoroType = new RoomEnvironmentalTypeBean.EnvironmentalType();
                            humanbodySenoroType.setDeviceName(device.getName());
                            humanbodySenoroType.setDeviceSerialNo(device.getDeviceSerialNo());
                            humanbodySenoroType.setType(device.getEnvironmentType());
                            humanbodySenoroType.setTypeName(getString(R.string.humanbody_senoro));
                            humanbodySenoroType.setTypeIcon(R.attr.icon_intelligent_environment_infrared_linkage);
                            environmentalTypes.add(humanbodySenoroType);
                        }
                        break;
                    case 22:
                        if(infraredContactType == null){
                            infraredContactType = new RoomEnvironmentalTypeBean.EnvironmentalType();
                            infraredContactType.setDeviceName(device.getName());
                            infraredContactType.setDeviceSerialNo(device.getDeviceSerialNo());
                            infraredContactType.setType(device.getEnvironmentType());
                            infraredContactType.setTypeName(getString(R.string.infrared_contrast));
                            infraredContactType.setTypeIcon(R.attr.icon_intelligent_environment_correlation_linkage);
                            environmentalTypes.add(infraredContactType);
                        }
                        break;
                    case 23:
                        if(vibrationSensingType == null){
                            vibrationSensingType = new RoomEnvironmentalTypeBean.EnvironmentalType();
                            vibrationSensingType.setDeviceName(device.getName());
                            vibrationSensingType.setDeviceSerialNo(device.getDeviceSerialNo());
                            vibrationSensingType.setType(device.getEnvironmentType());
                            vibrationSensingType.setTypeName(getString(R.string.vibration_sensing));
                            vibrationSensingType.setTypeIcon(R.attr.icon_intelligent_environment_shake_linkage);
                            environmentalTypes.add(vibrationSensingType);
                        }
                        break;
                    case 24:
                        if(infraredCurtainType == null){
                            infraredCurtainType = new RoomEnvironmentalTypeBean.EnvironmentalType();
                            infraredCurtainType.setDeviceName(device.getName());
                            infraredCurtainType.setDeviceSerialNo(device.getDeviceSerialNo());
                            infraredCurtainType.setType(device.getEnvironmentType());
                            infraredCurtainType.setTypeName(getString(R.string.infrared_curtain));
                            infraredCurtainType.setTypeIcon(R.attr.icon_intelligent_environment_curtain_linkage);
                            environmentalTypes.add(infraredCurtainType);
                        }
                        break;
                }
            }
            return environmentalTypes;
        }
        return null;
    }

    @Override
    public StatusLayout getStatusLayout() {
        return mStatusLayout;
    }
}